package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.PhoneCodeDao;
import com.loveyukari.model.PhoneCode;

/**
*
*yangshaohua
*2017年1月23日下午7:54:39
**/

@Repository
public class JdbcPhoneCodeDao extends BaseDao implements PhoneCodeDao{

	private class PhoneCodeMapper implements RowMapper<PhoneCode>{

		public PhoneCode mapRow(ResultSet rs, int rowNum) throws SQLException {
			PhoneCode phoneCode = new PhoneCode();
			phoneCode.setId(rs.getInt("id"));
			phoneCode.setCode(rs.getString("code"));
			phoneCode.setPhone(rs.getString("phone"));
			phoneCode.setStatus(rs.getInt("status"));
			phoneCode.setTime(rs.getTimestamp("time"));
			phoneCode.setType(rs.getString("type"));
			return phoneCode;
		}
		
	}

	public void insertPhoneCode(PhoneCode phoneCode) {
		String sql = "insert into phone_code (code,phone,type) values (?,?,?)";
		this.getJdbcTemplate().update(sql,phoneCode.getCode(),phoneCode.getPhone(),phoneCode.getType());
	}

	public PhoneCode selectPhoneCode(String phone, String code, String type) {
		String sql = "select * from phone_code where phone = ? and code = ? and type = ? and status = 0 order by time desc";
		List<PhoneCode> list = this.getJdbcTemplate().query(sql,new PhoneCodeMapper(),phone,code,type);
		if (!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	public void updatePhoneCodeStatus(int id, int status) {
		String sql = "update phone_code set status = ? where id = ?";
		this.getJdbcTemplate().update(sql,status,id);
	}
	


	

}
